<template>
  <div class="account-page">
    <el-form
      ref="userForm"
      :model="userInfo"
      :rules="rules"
      label-width="120px"
      style="margin-left: 120px; margin-top:30px"
    >
      <el-form-item prop="username" label="姓名:">
        <el-input v-model="userInfo.username" placeholder="请输入姓名" style="width:300px" />
      </el-form-item>
      <el-form-item prop="newPassword" label="新密码:">
        <el-input
          v-model="userInfo.newPassword"
          placeholder="请输入新密码"
          style="width:300px"
          type="password"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveUser">更新</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { reqGetUserDetailById, reqSaveUserDetailById } from '@/api/employee'
export default {
  name: 'AccountView',
  components: {},
  props: {},
  data () {
    return {
      userInfo: {
        username: '',
        newPassword: ''
      },
      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: ['blur', 'change'] }
        ],
        newPassword: [
          { min: 6, max: 9, message: '密码必须是6-9位', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  computed: {
    userId () {
      return this.$route.query.id
    }
  },
  watch: {},
  async created () {
    const { data } = await reqGetUserDetailById(this.userId)
    this.userInfo = data
  },
  mounted () {},
  methods: {
    saveUser () {
      this.$refs.userForm.validate(async (flag) => {
        if (!flag) return
        // 校验通过
        await reqSaveUserDetailById({
          ...this.userInfo,
          password: this.userInfo.newPassword
        })
        this.$message.success('保存成功')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
